﻿'
' Copyright (c) 2010-2012, MatthiWare
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of this software and associated documentation files (the "Software"),
' to deal in the Software without restriction, including without limitation
' the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software. 
' You shall include 'MatthiWare' in the credits/about section of the program
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'
Imports SharpDX

Imports System.IO
Public Module Utils

    Public Function Contains(rect1 As DrawingRectangleF, rect2 As DrawingRectangleF) As Boolean
        If (rect1.X > rect2.X And rect1.X < rect2.X + rect2.Width And rect1.Y > rect2.Y And rect2.Y < rect2.Y + rect2.Height) Or
            (rect2.X > rect1.X And rect2.X < rect1.X + rect1.Width And rect2.Y > rect1.Y And rect2.Y < rect1.Y + rect1.Height) Then
            Return True
        End If
        Return False
    End Function

    Public Function GetPlayerSizeBlocks(x As Double, y As Double, World As World) As Block()
        Dim b1 As Block = Nothing
        Dim b2 As Block = Nothing
        Dim b3 As Block = Nothing
        Dim b4 As Block = Nothing
        Dim b5 As Block = Nothing
        Dim b6 As Block = Nothing

        If (x > 0 And x < World.WorldWidth And y > 0 And y < World.WorldHeight) Then b1 = World.level(x, y)
        If (x + 1 > 0 And x + 1 < World.WorldWidth And y > 0 And y < World.WorldHeight) Then b2 = World.level(x + 1, y)
        If (x > 0 And x < World.WorldWidth And y + 1 > 0 And y + 1 < World.WorldHeight) Then b3 = World.level(x, y + 1)
        If (x > 0 And x < World.WorldWidth And y + 1 > 0 And y + 1 < World.WorldHeight) Then b4 = World.level(x + 1, y + 1)
        If (x > 0 And x < World.WorldWidth And y + 2 > 0 And y + 2 < World.WorldHeight) Then b5 = World.level(x, y + 2)
        If (x + 1 > 0 And x + 1 < World.WorldWidth And y + 2 > 0 And y + 2 < World.WorldHeight) Then b6 = World.level(x + 1, y + 2)

        Return New Block() {b1, b2, b3, b4, b5, b6}
    End Function

    Public Function GetBlocksUnderPlayer(e As Core.Base.GameCoreEventArgs) As Block()
        Dim world As World = e.Core.Services.GetService(GetType(World))
        Dim p As Player = e.Core.Services.GetService(GetType(Player))
        Dim b1 As Block = Nothing
        Dim b2 As Block = Nothing

        If (Math.Floor(p.Y) < world.WorldHeight And Math.Floor(p.Y) > 0) Then b1 = world.level(p.X, Math.Floor(p.Y + 2.875))
        If ((p.X + 1) < world.WorldWidth And Math.Floor(p.Y) < world.WorldHeight And Math.Floor(p.Y) > 0) Then b2 = world.level(p.X + 1, Math.Floor(p.Y + 2.875))

        Return New Block() {b1, b2}
    End Function

    Public Function GetBlocksRightPlayer(e As Core.Base.GameCoreEventArgs) As Block()
        Dim world As World = e.Core.Services.GetService(GetType(World))
        Dim p As Player = e.Core.Services.GetService(GetType(Player))
        Dim b1 As Block = Nothing
        Dim b2 As Block = Nothing
        Dim b3 As Block = Nothing

        If (Math.Ceiling(p.X + 1) < world.WorldWidth And p.Y > 0) Then b1 = world.level(Math.Ceiling(p.X + 1), p.Y)
        If (Math.Ceiling(p.X + 1) < world.WorldWidth And Math.Floor(p.Y + 1) < world.WorldHeight And p.Y > 0) Then b2 = world.level(Math.Ceiling(p.X + 1), Math.Floor(p.Y + 1))
        If (Math.Ceiling(p.X + 1) < world.WorldWidth And Math.Floor(p.Y + 2) < world.WorldHeight And p.Y > 0) Then b3 = world.level(Math.Ceiling(p.X + 1), Math.Floor(p.Y + 2))

        Return New Block() {b1, b2, b3}
    End Function

    Public Function GetBlocksLeftPlayer(e As Core.Base.GameCoreEventArgs) As Block()
        Dim world As World = e.Core.Services.GetService(GetType(World))
        Dim p As Player = e.Core.Services.GetService(GetType(Player))
        Dim b1 As Block = Nothing
        Dim b2 As Block = Nothing
        Dim b3 As Block = Nothing

        If (Math.Floor(p.X) >= 0 And p.Y > 0) Then b1 = world.level(Math.Floor(p.X), p.Y)
        If (Math.Floor(p.X) >= 0 And Math.Floor(p.Y + 1) < world.WorldWidth And p.Y > 0) Then b2 = world.level(Math.Floor(p.X), Math.Floor(p.Y + 1))
        If (Math.Floor(p.X) >= 0 And Math.Floor(p.Y + 2) < world.WorldWidth And p.Y > 0) Then b3 = world.level(Math.Floor(p.X), Math.Floor(p.Y + 2))

        Return New Block() {b1, b2, b3}
    End Function

    Public Function GetBlocksAbovePlayer(e As Core.Base.GameCoreEventArgs) As Block()
        Dim world As World = e.Core.Services.GetService(GetType(World))
        Dim p As Player = e.Core.Services.GetService(GetType(Player))
        Dim b1 As Block = Nothing
        Dim b2 As Block = Nothing

        If ((p.Y - 1) >= 0) Then b1 = world.level(p.X, p.Y - 1)
        If ((p.X + 1) < world.WorldWidth And (p.Y - 1) >= 0) Then b2 = world.level(p.X + 1, p.Y - 1)

        Return New Block() {b1, b2}
    End Function

    'Public Sub UnsafeCheckPaths()
    '    Dim strLogs As String = ".\Logs\"
    '    Dim strContent As String = ".\Content\"
    '    Dim strSave As String = ".\Saved Games\"

    '    If (Not Directory.Exists(strLogs)) Then Directory.CreateDirectory(strLogs)
    '    If (Not Directory.Exists(strContent)) Then Throw New DirectoryNotFoundException("The " & strContent & " directory is missing")
    '    If (Not Directory.Exists(strSave)) Then Directory.CreateDirectory(strSave)
    'End Sub

End Module
